package net.zoneland.knowledge.service.impl;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import net.zoneland.knowledge.model.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.google.common.base.Preconditions;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.ImmutableList;

import net.zoneland.knowledge.constant.Constants;
import net.zoneland.knowledge.controller.bean.UomUserOrgQueryVo;
import net.zoneland.knowledge.dao.BranchBrandMapper;
import net.zoneland.knowledge.dao.GridDutyCommandosMapper;
import net.zoneland.knowledge.dao.PartyGroupMapper;
import net.zoneland.knowledge.dao.PartyOrgLeaderMapper;
import net.zoneland.knowledge.dao.PartyOrgMapper;
import net.zoneland.knowledge.dao.PartyUserMapper;
import net.zoneland.knowledge.dao.VirtualGroupMapper;
import net.zoneland.knowledge.enums.LeaderDeptEnum;
import net.zoneland.knowledge.enums.PurviewEnum;
import net.zoneland.knowledge.service.PartyService;

/**
 * 党员/党组织业务接口实现.
 * @author zmq
 * 2022/8/26 16:20
 */
@Service
public class PartyServiceImpl implements PartyService {

    /**
     * 日志.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(PartyServiceImpl.class);

    /**
     * 党组织缓存键名称.
     */
    private static final String PARTY_ORG_CACHE_NAME = "orgCache";

    /**
     * 选人框根节点组织名称.
     */
    @Value("${party.orgRootName:浙江公司党委}")
    private String orgRootName;

    /**
     * 可以查看全部人信息的领导
     */
    @Value("${all.user.leader}")
    private String allUserLeader;

    /**
     * 开始时间不能为空信息.
     */
    private static final String BEGINTIME_MUST_NOT_EMPTY_MSG = "开始时间不能为空！";

    /**
     * 类型:责任区.
     */
    private static final String GDC_TYPE_DUTY = "DUTY";

    /**
     * 类型:突击队.
     */
    private static final String GDC_TYPE_COMMANDOS = "COMMANDOS";

    /**
     * 类型:网络.
     */
    private static final String GDC_TYPE_GRID = "GRID";

    /**
     * 党员数据接口.
     */
    @Autowired
    private PartyUserMapper partyUserMapper;

    /**
     * 党组织数据接口.
     */
    @Autowired
    private PartyOrgMapper partyOrgMapper;

    /**
     * 党小组织数据接口.
     */
    @Autowired
    private PartyGroupMapper partyGroupMapper;

    /**
     * 群组接口
     */
    @Autowired
    private VirtualGroupMapper virtualGroupMapper;

    /**
     * 网格，责任区，突击队信息查询接口.
     */
    @Autowired
    private GridDutyCommandosMapper gridDutyCommandosMapper;

    @Autowired
    private BranchBrandMapper branchBrandMapper;

    @Autowired
    private PartyOrgLeaderMapper partyOrgLeaderMapper;

    /**
     * 可以看所有领导简版的权限
     */
    private static final List<PurviewEnum> LEADER_SIMPLE_LIST = ImmutableList.of(PurviewEnum.BRANCH_DW,
            PurviewEnum.PROVINCE_DW, PurviewEnum.PROVINCE_PARTY_OFFICE_SECRETARY);

    /**
     * 可以看同公司领导的权限
     */
    private static final List<PurviewEnum> COMPANY_LEADER_SIMPLE_LIST = ImmutableList.of(PurviewEnum.SECRETARY,
            PurviewEnum.BRANCH_PARTY_OFFICE_SECRETARY);

    /**
     * 可以看全部人全部信息的权限
     */
    private static final List<PurviewEnum> ALL_USER_LIST = ImmutableList.of(PurviewEnum.PROVINCE_LEADER,
            PurviewEnum.PROVINCE_DW, PurviewEnum.PROVINCE_PARTY_OFFICE_SECRETARY);

    /**
     * 可以看同公司全部信息的权限
     */
    private static final List<PurviewEnum> ALL_COMPANY_LIST = ImmutableList.of(PurviewEnum.BRANCH_DW,
            PurviewEnum.BRANCH_LEADER, PurviewEnum.BRANCH_PARTY_OFFICE_SECRETARY);

    /**
     * 可以看同组织全部信息的权限
     */
    private static final List<PurviewEnum> ALL_PARTY_LIST = ImmutableList.of(PurviewEnum.SECRETARY,
            PurviewEnum.GANSHI, PurviewEnum.ORDINARY);

    /**
     * 可以看同组织支部品牌的权限
     */
    private static final List<PurviewEnum> ALL_PARTY_BRANDS_LIST = ImmutableList.of(PurviewEnum.SECRETARY,
            PurviewEnum.GANSHI, PurviewEnum.PROVINCE_PARTY_OFFICE_SECRETARY, PurviewEnum.BRANCH_PARTY_OFFICE_SECRETARY);

    /**
     * 可以看同公司组织支部品牌的权限
     */
    private static final List<PurviewEnum> ALL_COMPANY_BRANDS_LIST = ImmutableList.of(PurviewEnum.BRANCH_DW,
            PurviewEnum.BRANCH_LEADER);

    /**
     * 可以看全部支部品牌的权限
     */
    private static final List<PurviewEnum> ALL_BRANDS_LIST =
            ImmutableList.of(PurviewEnum.PROVINCE_DW,
            PurviewEnum.PROVINCE_LEADER);

    /**
     * guava缓存.
     * 缓存党组织：党组织数量总共约300多条
     */
    private static final Cache<String, Object> cache =
            CacheBuilder.newBuilder().expireAfterWrite(30, TimeUnit.MINUTES).build();

    /**
     * 查询党员.
     *
     * @param condition 查询条件
     * @return 党员集合
     */
    @Override
    public List<PartyUser> searchByCondition(final UomUserOrgQueryVo condition) {
        final int page = Math.max(1, condition.getPage());
        final int pageSize = condition.getSize() > 0 ? condition.getSize() : Constants.DEFAULT_DB_PAGE_SIZE;
        PageHelper.startPage(page, pageSize);
        final String queryText = condition.getUserName();
        if (StringUtils.isNotBlank(condition.getUserName())) {
            condition.setUserName(Constants.STRING_PERCENT + condition.getUserName() + Constants.STRING_PERCENT);
        }
        List<PartyUser> partyUsers = partyUserMapper.search(condition);
        // 如果精确匹配则只需要返回一个结果
        final List<PartyUser> matchExactUsers = partyUsers.stream()
            .filter(item -> matchExactPartyUser(item, queryText))
            /*
            精确匹配的党员matchExactUsers只有一个时才会使用，匹配结果大于1时抛弃不再使用，匹配两个结果和多于两个结果效果一样
            limit(2)可以提高性能，matchExactUsers size到达2时中断流立即返回,减少计算量
             */
            .limit(2)
            .collect(Collectors.toList());
        if (CollectionUtils.size(matchExactUsers) == 1) {
            partyUsers = matchExactUsers;
        }

        final LoginUserPojo userPojo = condition.getUserPojo();
        if (!condition.isIgnoreHiddenInfo()) {
            hiddenFieldForProvinceLeader(partyUsers, userPojo);
        }

        // 当查询的党员只一个时，页面是显示党员详情，需要查询：所辖责任区、所辖突击队等,领导不需要这些数据。
        if (CollectionUtils.size(partyUsers) == 1 && !partyUsers.get(0).getProvinceLeader() && !condition.isDoNotfillMore()) {
            fillMorePartyUserInfo(partyUsers.get(0), userPojo);
        }

        return partyUsers;
    }

    /**
     * 校验是否需要全部信息。
     * @param partyUser
     * @param userPojo
     * @return
     */
    private boolean checkNeedAll(final PartyUser partyUser, final LoginUserPojo userPojo) {

        //权限校验。如果没有传当前角色，默认全部查询。
        //如果查询就是当前员工的信息，则需要显示全部信息
        //大领导可以查看全部的信息
        //如果是查看党员的是领导，就只有简版信息
        //如果是查询权限是省公司党办书记，省公司管理员，省公司党委，就需要显示全部信息。
        //如果是查询权限是地市公司党办书记，地市公司管理员，地市公司党委，就需要显示本公司的全部信息。
        //如果查询权限是支部书记，干事，只有跟自己一个党支部的人需要显示全部信息。
        if (userPojo == null
                || checkAllUserLeader(userPojo.getUserID())
                || StringUtils.equals(userPojo.getUserID(), partyUser.getOaId())) {

            return true;
        }

        final List<PurviewEnum> purviewEnums = userPojo.getPurviewEnums();
        if (checkProvinceOffice(purviewEnums, partyUser)) {
            return true;
        }

        if(LeaderDeptEnum.checkIsLeader(partyUser.getDeptDn())) {
            return false;
        }

        return  checkAll(ALL_USER_LIST, purviewEnums, null, null)
                || checkAll(ALL_COMPANY_LIST, purviewEnums, partyUser.getCompanyDn(), userPojo.getCompanyDN())
                || checkAll(ALL_PARTY_LIST, purviewEnums, partyUser.getOrgUUID(), userPojo.getPartyOrgUuid())
                || checkProvinceOffice(purviewEnums, partyUser);
    }

    /**
     * 省公司党办书记，可以查市公司的领导
     * @param purviewEnums
     * @param partyUser
     * @return
     */
    private boolean checkProvinceOffice(final List<PurviewEnum> purviewEnums, final PartyUser partyUser) {
        return CollectionUtils.containsAny(purviewEnums, PurviewEnum.PROVINCE_PARTY_OFFICE_SECRETARY, PurviewEnum.PROVINCE_DW)
                && !StringUtils.equals(partyUser.getDeptDn(), LeaderDeptEnum.ZMCC.getDeptDn());
    }

    /**
     * 校验是否是有全部权限的大领导
     * @param userId
     * @return
     */
    private boolean checkAllUserLeader(final String userId){
        final List<String> list = Arrays.asList(StringUtils.split(allUserLeader, Constants.STRING_SPLIT));
        return CollectionUtils.containsAny(list, userId);
    }

    /**
     * 权限校验通用方法
     * @param checkPurviewEnums 用来校验的枚举
     * @param userPurviewEnums 登陆人的枚举
     * @param checkData 用来校验的字段
     * @param userData 登陆人对应的字段
     * @return
     */
    private boolean checkAll(final List<PurviewEnum> checkPurviewEnums, final List<PurviewEnum> userPurviewEnums,
                             final String checkData, final String userData) {
        return CollectionUtils.containsAny(checkPurviewEnums, userPurviewEnums)
                && StringUtils.equals(checkData, userData);
    }


    /**
     * 是否精确匹配.
     * @param partyUser 党员
     * @param queryText 查询关键字
     * @return boolean 是否精确匹配
     */
    private boolean matchExactPartyUser(final PartyUser partyUser, final String queryText) {
        return StringUtils.equals(partyUser.getDisplayName(), queryText)
               // 员工编号开始的e不区分大小写
               || StringUtils.equalsIgnoreCase(partyUser.getYgId(), queryText)
               || StringUtils.equals(partyUser.getMobile(), queryText);
    }

    /**
     * 补全更多党员需要的信息.
     * @param partyUser 党员信息
     */
    private void fillMorePartyUserInfo(final PartyUser partyUser, final LoginUserPojo userPojo) {

        final List<GridDutyCommandosUser> gdcUsers = gridDutyCommandosMapper.
                getUserGdcInfoByYgIdAndGdcType(partyUser.getYgId(), null);
        final Map<String, List<GridDutyCommandosUser>> dgcTypeInfoMap = gdcUsers.stream().
                collect(Collectors.groupingBy(GridDutyCommandosUser::getGdcType));
        if (checkNeedAll(partyUser, userPojo)) {
            partyUser.setGridList(dgcTypeInfoMap.getOrDefault(GDC_TYPE_GRID, Collections.emptyList()));
            partyUser.setPartyOrg(partyOrgMapper.getOrgByUuid(partyUser.getOrgUUID()));
        } else {
            hiddenFieldForSimple(partyUser);
            partyUser.setSimpleData(true);
        }
        partyUser.setDutyList(dgcTypeInfoMap.getOrDefault(GDC_TYPE_DUTY, Collections.emptyList()));
        partyUser.setCommandosList(dgcTypeInfoMap.getOrDefault(GDC_TYPE_COMMANDOS, Collections.emptyList()));
    }

    private void hiddenFieldForProvinceLeader(final List<PartyUser> partyUsers, final LoginUserPojo userPojo) {
        partyUsers.stream().forEach(item -> {
            if (LeaderDeptEnum.checkIsLeader(item.getDeptDn()) && !checkNeedSimple(item, userPojo)) {
                item.setProvinceLeader(true);
                hiddenFieldFormProvinceLeader(item);
            }
        });
    }

    /**
     * 校验是否是简版信息。否表示最简版信息
     * @param partyUser
     * @param userPojo
     * @return
     */
    private boolean checkNeedSimple(final PartyUser partyUser, final LoginUserPojo userPojo) {
        return userPojo != null
                && (
                    CollectionUtils.containsAny(LEADER_SIMPLE_LIST, userPojo.getPurviewEnums())
                    || checkAll(COMPANY_LEADER_SIMPLE_LIST, userPojo.getPurviewEnums(),
                                partyUser.getCompanyDn(), userPojo.getCompanyDN())
                    || checkProvinceLeader(partyUser, userPojo)
                );
    }

    /**
     * 省公司管理员校验
     * @param partyUser
     * @param userPojo
     * @return
     */
    private boolean checkProvinceLeader(final PartyUser partyUser, final LoginUserPojo userPojo) {
        return CollectionUtils.containsAny(userPojo.getPurviewEnums(), PurviewEnum.PROVINCE_LEADER)
                && !StringUtils.equals(partyUser.getDeptDn(), LeaderDeptEnum.ZMCC.getDeptDn());
    }

    /**
     * 隐藏公司领导的字段.
     * @param user 党员信息
     */
    private void hiddenFieldFormProvinceLeader(final PartyUser user) {
        PartyUser partyUser = new PartyUser();
        partyUser.setAdministrativeDuties(user.getAdministrativeDuties());
        partyUser.setDisplayName(user.getDisplayName());
        partyUser.setYgId(user.getYgId());
        partyUser.setProvinceLeader(user.getProvinceLeader());
        partyUser.setOrgUUID(user.getOrgUUID());
        partyUser.setOrgName(user.getOrgName());
        partyUser.setOrgShortName(user.getOrgShortName());
        partyUser.setShortName2(user.getShortName2());
        partyUser.setPositionForDisplay(user.getPositionForDisplay());
        partyUser.setFavoriteStatus(user.isFavoriteStatus());
        BeanUtils.copyProperties(partyUser, user);
    }

    /**
     * 隐藏字段，只保留简版信息
     * @param partyUser
     */
    private void hiddenFieldForSimple(final PartyUser partyUser) {
        final String mobile = partyUser.getMobile();
        final String email = partyUser.getEmail();
        final Boolean provinceLeader = partyUser.getProvinceLeader();
        hiddenFieldFormProvinceLeader(partyUser);
        partyUser.setMobile(mobile);
        partyUser.setEmail(email);
        partyUser.setProvinceLeader(provinceLeader);
    }

    /**
     * 查询党组织.
     *
     * @param orgName 组织名称
     * @param page     页
     * @param pageSize 每页记录数
     * @return 党组织集合
     */
    @Override
    public List<PartyOrg> searchByOrgName(final String orgName, final int page, final int pageSize, final LoginUserPojo user) {
        Preconditions.checkArgument(StringUtils.isNotBlank(orgName), "党组织名称不能为空！");
        PageHelper.startPage(1, pageSize > 0 ? pageSize : Constants.DEFAULT_DB_PAGE_SIZE);
        final List<PartyOrg> partyOrgs = partyOrgMapper.search(Constants.STRING_PERCENT + orgName + Constants.STRING_PERCENT, user.getUserID());
        /*
          当查询的党组织只有一个时，页面是显示详情，需要查询：
          所属支部品牌、所辖责任区、所辖突击队
         */
        if (CollectionUtils.size(partyOrgs) == 1) {
            fillMorePartyOrgInfo(partyOrgs.get(0), user);
        }

        return partyOrgs;
    }

    /**
     * 补全更多党组织需要的信息.
     * @param partyOrg 党组织实体
     */
    private void fillMorePartyOrgInfo(final PartyOrg partyOrg, final LoginUserPojo user) {

        String orgUUID = partyOrg.getOrgUUID();
        final List<GridDutyCommandosOrg> gdcOrgs = gridDutyCommandosMapper.
            getOrgGdcInfoByOrgUuidAndGdcType(orgUUID, null);
        final Map<String, List<GridDutyCommandosOrg>> dgcTypeInfoMap = gdcOrgs.stream().
            collect(Collectors.groupingBy(GridDutyCommandosOrg::getGdcType));
        partyOrg.setDutyList(dgcTypeInfoMap.getOrDefault(GDC_TYPE_DUTY, Collections.emptyList()));
        partyOrg.setCommandosList(dgcTypeInfoMap.getOrDefault(GDC_TYPE_COMMANDOS, Collections.emptyList()));
        partyOrg.setBranchBrandList(branchBrandMapper.getBranchBrandByOrgUuid(orgUUID));
        final List<BranchBrand> branchBrands = branchBrandMapper.getBranchBrandsByOrgUuid(orgUUID);
        if (!checkHasBranchBrands(partyOrg, user)) {
            branchBrands.forEach(branchBrand -> {
                branchBrand.setPiid(null);
            });
        }
        partyOrg.setBranchBrands(branchBrands);
        // 获取先锋岗用户
        partyOrg.setPioneerPostUsers(partyUserMapper.orgPioneerPostUsers(orgUUID));
        partyOrg.setOrgLeader(partyOrgLeaderMapper.queryByOrgUuid(orgUUID));
    }

    /**
     * 校验党支部能否查看支部品牌
     * @param partyOrg
     * @param user
     * @return
     */
    private boolean checkHasBranchBrands(final PartyOrg partyOrg, final LoginUserPojo user) {
        return user == null
                || checkAll(ALL_BRANDS_LIST, user.getPurviewEnums(), null, null)
                || checkAll(ALL_COMPANY_BRANDS_LIST, user.getPurviewEnums(), partyOrg.getCompanyDn(), user.getCompanyDN())
                || checkAll(ALL_PARTY_BRANDS_LIST, user.getPurviewEnums(), partyOrg.getOrgUUID(), user.getPartyOrgUuid());
    }

    @Override
    public List<PartyUser> getUserByTime(final String beginTime) {
        Preconditions.checkArgument(StringUtils.isNotBlank(beginTime), BEGINTIME_MUST_NOT_EMPTY_MSG);
        final List<PartyUser> partyUsers = partyUserMapper.getUserByTime(beginTime);
        if (CollectionUtils.size(partyUsers) == 1) {
            fillMorePartyUserInfo(partyUsers.get(0), null);
        }
        hiddenFieldForProvinceLeader(partyUsers, null);
        return partyUsers;
    }

    @Override
    public List<PartyOrg> getOrgByTime(final String beginTime) {
        Preconditions.checkArgument(StringUtils.isNotBlank(beginTime), BEGINTIME_MUST_NOT_EMPTY_MSG);
        final List<PartyOrg> partyOrgs = partyOrgMapper.getOrgByTime(beginTime);
        if (CollectionUtils.size(partyOrgs) == 1) {
            fillMorePartyOrgInfo(partyOrgs.get(0), null);
        }
        return partyOrgs;
    }

    @Override
    public PartyOrg getOrgByUuid(final String orgUUID) {
        return partyOrgMapper.getOrgByUuid(orgUUID);
    }

    @Override
    public PartyGroup getGroupByUuid(final String orgGroupUUID) {
        return partyGroupMapper.getOrgByUuid(orgGroupUUID);
    }

    @Override
    public List<PartyOrgVoForSelect> searchOrgByConditionForTree(final UomUserOrgQueryVo condition) {
        List<PartyOrgVoForSelect> partyOrgs = (List<PartyOrgVoForSelect>)cache.getIfPresent(PARTY_ORG_CACHE_NAME);
        if (CollectionUtils.isEmpty(partyOrgs)) {
            partyOrgs = initCacheByKey(PARTY_ORG_CACHE_NAME);
            cache.put(PARTY_ORG_CACHE_NAME, partyOrgs);
        }
        //树返回时需要标记是否是叶子节点，先把所有父id取出来，id是父Id的说明是非叶子节点
        final Set<String> allParentOrgUuid = partyOrgs.stream()
            .map(PartyOrgVoForSelect::getParentOrgUuid)
            .collect(Collectors.toSet());
        // 党组织因为数据量不大，所以缓存在内存，如果有查询条件，需要使用程序方式过滤
        // filter中实现sql中的逻辑
        return partyOrgs.stream().filter(item -> {
            boolean flag = true;
            if (StringUtils.isNotBlank(condition.getParentOrgUuid())) {
                if (!StringUtils.equals(item.getParentOrgUuid(), condition.getParentOrgUuid())) {
                    return false;
                }
            }
            if (StringUtils.isNotBlank(condition.getQueryText())) {
                if (!StringUtils.contains(item.getOrgShortName(), condition.getQueryText())) {
                    return false;
                }
            }
            return flag;
        }).map(item -> {
            //判断是否是叶子节点
            item.setLeaf(!allParentOrgUuid.contains(item.getOrgUUID()));
            return item;
        }).collect(Collectors.toList());
    }

    @Override
    public List<PartyUserVoForSelect> searchUserByConditionForTree(final UomUserOrgQueryVo condition) {
        if (StringUtils.isNotBlank(condition.getQueryText())) {
            condition.setQueryTextForMobile(condition.getQueryText());
            condition.setQueryTextForYgId(StringUtils.upperCase(condition.getQueryText()));
            condition.setQueryText(Constants.STRING_PERCENT + condition.getQueryText() + Constants.STRING_PERCENT);
        }
        return partyUserMapper.searchUserByConditionForTree(condition);
    }

    @Override
    public List<VirtualGroupName> queryGroupName(final String userId) {
        return Optional.ofNullable(userId)
                .filter(StringUtils::isNotBlank)
                .map(virtualGroupMapper::queryGroupNameByUid)
                .orElseGet(Collections::emptyList);

    }

    @Override
    public List<VirtualGroup> queryGroupMemberByGroupId(final String groupId) {
        return Optional.ofNullable(groupId)
                .filter(StringUtils::isNotBlank)
                .map(virtualGroupMapper::queryGroupMemberByGroupId)
                .orElseGet(Collections::emptyList);
    }

    private List<PartyOrgVoForSelect> initCacheByKey(final String key) {
        // 目前就一种缓存类型
        if (StringUtils.equals(PARTY_ORG_CACHE_NAME, key)) {
            return partyOrgMapper.searchOrgByConditionForTree(new UomUserOrgQueryVo());
        }
        return Collections.emptyList();
    }

    @Override
    public List<PartyUser> getGanshiByOrgUuid(final String orgUuid) {
        if (StringUtils.isBlank(orgUuid)) {
            return Collections.emptyList();
        }
        return partyUserMapper.getGanshiByOrgUuid(orgUuid);
    }

    /**
     * 根据公司信息查询三会一课信息.
     * @param companyDn 公司编号
     * @return 返回三会一课反向查询列表.
     */
    @Override
    public List<SanKeReverse> getOrg(final String companyDn) {
        return partyOrgMapper.getOrg(companyDn);
    }

    /**
     * 根据员工编号查询党员信息.
     * @param empNo 员工编号
     * @param mobile 手机号
     * @return
     */
    @Override
    public PartyUser queryPartyUserByEmpNoOrMobile(final String empNo, final String mobile) {
        if (StringUtils.isNotBlank(empNo)) {
            return this.findPartyUserByEmpNo(empNo);
        }
        return this.findPartyUserByMobile(mobile);
    }

    /**
     * 根据员工编号查询党员信息
     * @param empNo 员工编号
     * @return
     */
    @Override
    public PartyUser findPartyUserByEmpNo(final String empNo) {
        return partyUserMapper.findPartyUserByEmpNo(empNo);
    }

    /**
     * 根据手机号查询党员信息
     * @param mobile 员工手机号
     * @return
     */
    @Override
    public PartyUser findPartyUserByMobile(final String mobile) {
        return partyUserMapper.findPartyUserByMobile(mobile);
    }

    /**
     * 查询党员手机号.
     *
     * @param conditionList 查询条件
     * @return 手机列表
     */
    @Override
    public List<String> searchMobileListByCondition(final List<UomUserOrgQueryVo> conditionList) {
        return conditionList.stream()
                //构建用户组织信息查询参数 并查询手机号列表
                .map(this::fillUserAndQueryMobiles)
                .flatMap(Collection::stream)
                .collect(Collectors.toList());
    }

    /**
     * 构建查询参数并根据参数查询.
     * @param uomUserOrgQueryVo  用户组织信息的查询参数对象.
     * @return 返回手机号列表.
     */
    private List<String> fillUserAndQueryMobiles(final UomUserOrgQueryVo uomUserOrgQueryVo) {
        final String queryText = uomUserOrgQueryVo.getQueryText();
        //如果有输入参数，填充到用户组织查询对象里面.
        if (StringUtils.isNotBlank(queryText)) {
            uomUserOrgQueryVo.setQueryTextForMobile(queryText);
            uomUserOrgQueryVo.setQueryTextForYgId(StringUtils.upperCase(queryText));
            uomUserOrgQueryVo.setQueryText(Constants.STRING_PERCENT + queryText + Constants.STRING_PERCENT);
        }
        //根据用户组织查询对象查询该用户手机信息.
        return partyUserMapper.searchUserMobilesByCondition(uomUserOrgQueryVo);
    }

}
